package com.example.leo.mycharmingstudent.a0914;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;

public class AActivity extends AppCompatActivity {

    private static final String TAG = "AActivity";

    //启动B activity 的请求码
    public static final int START_B_ACTIVITY_REQUEST_CODE = 1000;
    //启动B activity按钮
    private Button btnStartB;
    //启动B activity并使用Serializable序列化传递参数
    private Button btnSerializable;
    //启动B activity并使用Parcelable序列化传递参数
    private Button btnParcelable;
    private ImageView ivImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        findViews();
        initData();
        initEvent();

    }

    private void findViews() {
        btnStartB = findViewById(R.id.btn_start_b_activity_a);
        btnSerializable = findViewById(R.id.btn_start_b_with_serializable_activity_a);
        btnParcelable = findViewById(R.id.btn_start_b_with_parcelable_activity_a);
        ivImage = findViewById(R.id.iv_image_activity_a);
    }

    private void initData() {
        Glide.with(this)
                .load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600138695061&di=2445ac0688084d480ce55549760469bf&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg")
                .thumbnail(0.01f)
                .placeholder(R.mipmap.hero0)
                .error(R.mipmap.hero0)
                .into(ivImage);
    }

    private void initEvent() {
        btnStartB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动B Activity
                Intent intent = new Intent(AActivity.this, BActivity.class);
                startActivityForResult(intent, START_B_ACTIVITY_REQUEST_CODE);
            }
        });
        btnSerializable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动B Activity 并使用Serializable传递数据
                Intent intent = new Intent();
                intent.setClass(AActivity.this, BActivity.class);
                StudentEntity studentEntity = new StudentEntity("王小二", "男", "40", "200kg", "180cm站在凳子上");
                intent.putExtra("student",studentEntity);
                startActivity(intent);
            }
        });
        btnParcelable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动B Activity 并使用Parcelable传递数据
                Intent intent = new Intent(AActivity.this, BActivity.class);
                StudentEntityParcelable parcelable = new StudentEntityParcelable();
                parcelable.name = "张三";
                parcelable.sex = "女";
                intent.putExtra("parcel",parcelable);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode,
                                    @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断请求码
        if (requestCode == START_B_ACTIVITY_REQUEST_CODE) {
            /*
            判断结果码
            结果码一般用两种值：1.成功 RESULT_OK  2.失败  RESULT_CANCELED
             */
            if (resultCode == RESULT_OK) {
                //判断intent对象是否为空
                if (data != null) {
                    String resultData = data.getStringExtra("data");
                    ToastUtil.showToast(this, resultData);
                }
            } else {
                Log.e(TAG, "onActivityResult: 结果码不对");
            }
        }
    }
}
